//
//  CLLabelView.m
//  MasonryTest
//
//  Created by Charles on 16/9/23.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLLabelView.h"

static UIEdgeInsets const kPadding = {10, 10, 10, 10};

@interface CLLabelView()

@property (strong,nonatomic) UILabel * shortLabel;
@property (strong,nonatomic) UILabel * longLabel;

@end

@implementation CLLabelView

- (instancetype)init{
    if (self = [super init]) {
        [self makeView];
    }
    return self;
}

- (void)makeView{
    self.shortLabel.numberOfLines = 1;
    self.shortLabel.textColor = [UIColor purpleColor];
    self.shortLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    self.shortLabel.text = @"Bacon";
    
    self.longLabel.numberOfLines = 8;
    self.longLabel.backgroundColor = [UIColor yellowColor];
    self.longLabel.textColor = [UIColor darkGrayColor];
    self.longLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    self.longLabel.text = @"Bacon ipsum dolor sit amet spare ribs fatback kielbasa salami, tri-tip jowl pastrami flank short loin rump sirloin. Tenderloin frankfurter chicken biltong rump chuck filet mignon pork t-bone flank ham hock.";
   

    [self.shortLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.longLabel.centerY);
        make.right.equalTo(self.right).insets(kPadding);
    }];
    
    [self.longLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.left).insets(kPadding);
        make.top.equalTo(self.top).insets(kPadding);
    }];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat width = CGRectGetMinX(self.shortLabel.frame) - kPadding.left;
    width -= CGRectGetMinX(self.longLabel.frame);
    self.longLabel.preferredMaxLayoutWidth = width;
    
    // need to layoutSubviews again as frames need to recalculated with preferredLayoutWidth
    [super layoutSubviews];

}


- (UILabel *)shortLabel{
    if (!_shortLabel) {
        _shortLabel = [UILabel new];
        [self addSubview:_shortLabel];
    }
    return _shortLabel;
}


- (UILabel *)longLabel{
    if (!_longLabel) {
        _longLabel = [UILabel new];
        [self addSubview:_longLabel];
    }
    return _longLabel;
}

@end
